3 namespace MediaWiki\Tests\Block
;
5 use MediaWiki\Block\BlockRestriction
;
6 use MediaWiki\Block\Restriction\NamespaceRestriction
;
7 use MediaWiki\Block\Restriction\PageRestriction
;
8 use MediaWiki\Block\Restriction\Restriction
;
13 * @coversDefaultClass \MediaWiki\Block\BlockRestriction
15 class BlockRestrictionTest
extends \MediaWikiLangTestCase
{
17 public function tearDown() {
23 * @covers ::loadByBlockId
24 * @covers ::resultToRestrictions
25 * @covers ::rowToRestriction
27 public function testLoadMultipleRestrictions() {
28 $this->setMwGlobals( [
29 'wgBlockDisablesLogin' => false,
31 $block = $this->insertBlock();
33 $pageFoo = $this->getExistingTestPage( 'Foo' );
34 $pageBar = $this->getExistingTestPage( 'Bar' );
36 BlockRestriction
::insert( [
37 new PageRestriction( $block->getId(), $pageFoo->getId() ),
38 new PageRestriction( $block->getId(), $pageBar->getId() ),
39 new NamespaceRestriction( $block->getId(), NS_USER
),
42 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
44 $this->assertCount( 3, $restrictions );
48 * @covers ::loadByBlockId
49 * @covers ::resultToRestrictions
50 * @covers ::rowToRestriction
52 public function testWithNoRestrictions() {
53 $block = $this->insertBlock();
54 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
55 $this->assertEmpty( $restrictions );
59 * @covers ::loadByBlockId
60 * @covers ::resultToRestrictions
61 * @covers ::rowToRestriction
63 public function testWithEmptyParam() {
64 $restrictions = BlockRestriction
::loadByBlockId( [] );
66 $this->assertEmpty( $restrictions );
70 * @covers ::loadByBlockId
71 * @covers ::resultToRestrictions
72 * @covers ::rowToRestriction
74 public function testIgnoreNotSupportedTypes() {
75 $block = $this->insertBlock();
77 $pageFoo = $this->getExistingTestPage( 'Foo' );
78 $pageBar = $this->getExistingTestPage( 'Bar' );
81 $this->insertRestriction( $block->getId(), PageRestriction
::TYPE_ID
, $pageFoo->getId() );
82 $this->insertRestriction( $block->getId(), NamespaceRestriction
::TYPE_ID
, NS_USER
);
85 $this->insertRestriction( $block->getId(), 9, $pageBar->getId() );
86 $this->insertRestriction( $block->getId(), 10, NS_FILE
);
88 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
89 $this->assertCount( 2, $restrictions );
93 * @covers ::loadByBlockId
94 * @covers ::resultToRestrictions
95 * @covers ::rowToRestriction
97 public function testMappingPageRestrictionObject() {
98 $block = $this->insertBlock();
99 $title = 'Lady Macbeth';
100 $page = $this->getExistingTestPage( $title );
102 // Test Page Restrictions.
103 BlockRestriction
::insert( [
104 new PageRestriction( $block->getId(), $page->getId() ),
107 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
109 list( $pageRestriction ) = $restrictions;
110 $this->assertInstanceOf( PageRestriction
::class, $pageRestriction );
111 $this->assertEquals( $block->getId(), $pageRestriction->getBlockId() );
112 $this->assertEquals( $page->getId(), $pageRestriction->getValue() );
113 $this->assertEquals( $pageRestriction->getType(), PageRestriction
::TYPE
);
114 $this->assertEquals( $pageRestriction->getTitle()->getText(), $title );
118 * @covers ::loadByBlockId
119 * @covers ::resultToRestrictions
120 * @covers ::rowToRestriction
122 public function testMappingNamespaceRestrictionObject() {
123 $block = $this->insertBlock();
125 BlockRestriction
::insert( [
126 new NamespaceRestriction( $block->getId(), NS_USER
),
129 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
131 list( $namespaceRestriction ) = $restrictions;
132 $this->assertInstanceOf( NamespaceRestriction
::class, $namespaceRestriction );
133 $this->assertEquals( $block->getId(), $namespaceRestriction->getBlockId() );
134 $this->assertSame( NS_USER
, $namespaceRestriction->getValue() );
135 $this->assertEquals( $namespaceRestriction->getType(), NamespaceRestriction
::TYPE
);
141 public function testInsert() {
142 $block = $this->insertBlock();
144 $pageFoo = $this->getExistingTestPage( 'Foo' );
145 $pageBar = $this->getExistingTestPage( 'Bar' );
149 new PageRestriction( $block->getId(), $pageFoo->getId() ),
150 new PageRestriction( $block->getId(), $pageBar->getId() ),
151 new NamespaceRestriction( $block->getId(), NS_USER
)
154 $result = BlockRestriction
::insert( $restrictions );
155 $this->assertTrue( $result );
161 $result = BlockRestriction
::insert( $restrictions );
162 $this->assertFalse( $result );
164 $result = BlockRestriction
::insert( [] );
165 $this->assertFalse( $result );
171 public function testInsertTypes() {
172 $block = $this->insertBlock();
174 $pageFoo = $this->getExistingTestPage( 'Foo' );
175 $pageBar = $this->getExistingTestPage( 'Bar' );
177 $invalid = $this->createMock( Restriction
::class );
178 $invalid->method( 'toRow' )
180 'ir_ipb_id' => $block->getId(),
187 new PageRestriction( $block->getId(), $pageFoo->getId() ),
188 new PageRestriction( $block->getId(), $pageBar->getId() ),
189 new NamespaceRestriction( $block->getId(), NS_USER
),
193 $result = BlockRestriction
::insert( $restrictions );
194 $this->assertTrue( $result );
196 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
197 $this->assertCount( 3, $restrictions );
202 * @covers ::restrictionsByBlockId
203 * @covers ::restrictionsToRemove
205 public function testUpdateInsert() {
206 $block = $this->insertBlock();
207 $pageFoo = $this->getExistingTestPage( 'Foo' );
208 $pageBar = $this->getExistingTestPage( 'Bar' );
209 BlockRestriction
::insert( [
210 new PageRestriction( $block->getId(), $pageFoo->getId() ),
213 BlockRestriction
::update( [
215 new PageRestriction( $block->getId(), $pageBar->getId() ),
216 new NamespaceRestriction( $block->getId(), NS_USER
),
219 $db = wfGetDb( DB_REPLICA
);
220 $result = $db->select(
221 [ 'ipblocks_restrictions' ],
223 [ 'ir_ipb_id' => $block->getId() ]
226 $this->assertEquals( 2, $result->numRows() );
227 $row = $result->fetchObject();
228 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
229 $this->assertEquals( $pageBar->getId(), $row->ir_value
);
234 * @covers ::restrictionsByBlockId
235 * @covers ::restrictionsToRemove
237 public function testUpdateChange() {
238 $block = $this->insertBlock();
239 $page = $this->getExistingTestPage( 'Foo' );
241 BlockRestriction
::update( [
242 new PageRestriction( $block->getId(), $page->getId() ),
245 $db = wfGetDb( DB_REPLICA
);
246 $result = $db->select(
247 [ 'ipblocks_restrictions' ],
249 [ 'ir_ipb_id' => $block->getId() ]
252 $this->assertEquals( 1, $result->numRows() );
253 $row = $result->fetchObject();
254 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
255 $this->assertEquals( $page->getId(), $row->ir_value
);
260 * @covers ::restrictionsByBlockId
261 * @covers ::restrictionsToRemove
263 public function testUpdateNoRestrictions() {
264 $block = $this->insertBlock();
266 BlockRestriction
::update( [] );
268 $db = wfGetDb( DB_REPLICA
);
269 $result = $db->select(
270 [ 'ipblocks_restrictions' ],
272 [ 'ir_ipb_id' => $block->getId() ]
275 $this->assertEquals( 0, $result->numRows() );
280 * @covers ::restrictionsByBlockId
281 * @covers ::restrictionsToRemove
283 public function testUpdateSame() {
284 $block = $this->insertBlock();
285 $page = $this->getExistingTestPage( 'Foo' );
286 BlockRestriction
::insert( [
287 new PageRestriction( $block->getId(), $page->getId() ),
290 BlockRestriction
::update( [
291 new PageRestriction( $block->getId(), $page->getId() ),
294 $db = wfGetDb( DB_REPLICA
);
295 $result = $db->select(
296 [ 'ipblocks_restrictions' ],
298 [ 'ir_ipb_id' => $block->getId() ]
301 $this->assertEquals( 1, $result->numRows() );
302 $row = $result->fetchObject();
303 $this->assertEquals( $block->getId(), $row->ir_ipb_id
);
304 $this->assertEquals( $page->getId(), $row->ir_value
);
308 * @covers ::updateByParentBlockId
310 public function testDeleteAllUpdateByParentBlockId() {
311 // Create a block and an autoblock (a child block)
312 $block = $this->insertBlock();
313 $pageFoo = $this->getExistingTestPage( 'Foo' );
314 $pageBar = $this->getExistingTestPage( 'Bar' );
315 BlockRestriction
::insert( [
316 new PageRestriction( $block->getId(), $pageFoo->getId() ),
318 $autoblockId = $block->doAutoblock( '127.0.0.1' );
320 // Ensure that the restrictions on the block have not changed.
321 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
322 $this->assertCount( 1, $restrictions );
323 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
325 // Ensure that the restrictions on the autoblock are the same as the block.
326 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
327 $this->assertCount( 1, $restrictions );
328 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
330 // Update the restrictions on the autoblock (but leave the block unchanged)
331 BlockRestriction
::updateByParentBlockId( $block->getId(), [
332 new PageRestriction( $block->getId(), $pageBar->getId() ),
335 // Ensure that the restrictions on the block have not changed.
336 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
337 $this->assertCount( 1, $restrictions );
338 $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() );
340 // Ensure that the restrictions on the autoblock have been updated.
341 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
342 $this->assertCount( 1, $restrictions );
343 $this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() );
347 * @covers ::updateByParentBlockId
349 public function testUpdateByParentBlockId() {
350 // Create a block and an autoblock (a child block)
351 $block = $this->insertBlock();
352 $page = $this->getExistingTestPage( 'Foo' );
353 BlockRestriction
::insert( [
354 new PageRestriction( $block->getId(), $page->getId() ),
356 $autoblockId = $block->doAutoblock( '127.0.0.1' );
358 // Ensure that the restrictions on the block have not changed.
359 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
360 $this->assertCount( 1, $restrictions );
362 // Ensure that the restrictions on the autoblock have not changed.
363 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
364 $this->assertCount( 1, $restrictions );
366 // Remove the restrictions on the autoblock (but leave the block unchanged)
367 BlockRestriction
::updateByParentBlockId( $block->getId(), [] );
369 // Ensure that the restrictions on the block have not changed.
370 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
371 $this->assertCount( 1, $restrictions );
373 // Ensure that the restrictions on the autoblock have been updated.
374 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
375 $this->assertCount( 0, $restrictions );
379 * @covers ::updateByParentBlockId
381 public function testNoAutoblocksUpdateByParentBlockId() {
382 // Create a block with no autoblock.
383 $block = $this->insertBlock();
384 $page = $this->getExistingTestPage( 'Foo' );
385 BlockRestriction
::insert( [
386 new PageRestriction( $block->getId(), $page->getId() ),
389 // Ensure that the restrictions on the block have not changed.
390 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
391 $this->assertCount( 1, $restrictions );
393 // Update the restrictions on any autoblocks (there are none).
394 BlockRestriction
::updateByParentBlockId( $block->getId(), $restrictions );
396 // Ensure that the restrictions on the block have not changed.
397 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
398 $this->assertCount( 1, $restrictions );
404 public function testDelete() {
405 $block = $this->insertBlock();
406 $page = $this->getExistingTestPage( 'Foo' );
407 BlockRestriction
::insert( [
408 new PageRestriction( $block->getId(), $page->getId() ),
411 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
412 $this->assertCount( 1, $restrictions );
414 $result = BlockRestriction
::delete( array_merge( $restrictions, [ new \
stdClass() ] ) );
415 $this->assertTrue( $result );
417 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
418 $this->assertCount( 0, $restrictions );
422 * @covers ::deleteByBlockId
424 public function testDeleteByBlockId() {
425 $block = $this->insertBlock();
426 $page = $this->getExistingTestPage( 'Foo' );
427 BlockRestriction
::insert( [
428 new PageRestriction( $block->getId(), $page->getId() ),
431 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
432 $this->assertCount( 1, $restrictions );
434 $result = BlockRestriction
::deleteByBlockId( $block->getId() );
435 $this->assertNotFalse( $result );
437 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
438 $this->assertCount( 0, $restrictions );
442 * @covers ::deleteByParentBlockId
444 public function testDeleteByParentBlockId() {
445 // Create a block with no autoblock.
446 $block = $this->insertBlock();
447 $page = $this->getExistingTestPage( 'Foo' );
448 BlockRestriction
::insert( [
449 new PageRestriction( $block->getId(), $page->getId() ),
451 $autoblockId = $block->doAutoblock( '127.0.0.1' );
453 // Ensure that the restrictions on the block have not changed.
454 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
455 $this->assertCount( 1, $restrictions );
457 // Ensure that the restrictions on the autoblock are the same as the block.
458 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
459 $this->assertCount( 1, $restrictions );
461 // Remove all of the restrictions on the autoblock (but leave the block unchanged).
462 $result = BlockRestriction
::deleteByParentBlockId( $block->getId() );
463 // NOTE: commented out until https://gerrit.wikimedia.org/r/c/mediawiki/core/+/469324 is merged
464 //$this->assertTrue( $result );
466 // Ensure that the restrictions on the block have not changed.
467 $restrictions = BlockRestriction
::loadByBlockId( $block->getId() );
468 $this->assertCount( 1, $restrictions );
470 // Ensure that the restrictions on the autoblock have been removed.
471 $restrictions = BlockRestriction
::loadByBlockId( $autoblockId );
472 $this->assertCount( 0, $restrictions );
477 * @dataProvider equalsDataProvider
481 * @param bool $expected
483 public function testEquals( array $a, array $b, $expected ) {
484 $this->assertSame( $expected, BlockRestriction
::equals( $a, $b ) );
487 public function equalsDataProvider() {
492 new PageRestriction( 1, 1 ),
496 new PageRestriction( 1, 2 )
502 new PageRestriction( 1, 1 ),
505 new PageRestriction( 1, 1 ),
506 new PageRestriction( 1, 2 )
517 new PageRestriction( 1, 1 ),
518 new PageRestriction( 1, 2 ),
519 new PageRestriction( 2, 3 ),
522 new PageRestriction( 2, 3 ),
523 new PageRestriction( 1, 2 ),
524 new PageRestriction( 1, 1 ),
530 new NamespaceRestriction( 1, NS_USER
),
533 new NamespaceRestriction( 1, NS_USER
),
539 new NamespaceRestriction( 1, NS_USER
),
542 new NamespaceRestriction( 1, NS_TALK
),
550 * @covers ::setBlockId
552 public function testSetBlockId() {
555 new PageRestriction( 1, 1 ),
556 new PageRestriction( 1, 2 ),
557 new NamespaceRestriction( 1, NS_USER
),
560 $this->assertSame( 1, $restrictions[1]->getBlockId() );
561 $this->assertSame( 1, $restrictions[2]->getBlockId() );
562 $this->assertSame( 1, $restrictions[3]->getBlockId() );
564 $result = BlockRestriction
::setBlockId( 2, $restrictions );
566 foreach ( $result as $restriction ) {
567 $this->assertSame( 2, $restriction->getBlockId() );
571 protected function insertBlock() {
572 $badActor = $this->getTestUser()->getUser();
573 $sysop = $this->getTestSysop()->getUser();
575 $block = new \
Block( [
576 'address' => $badActor->getName(),
577 'user' => $badActor->getId(),
578 'by' => $sysop->getId(),
579 'expiry' => 'infinity',
581 'enableAutoblock' => true,
589 protected function insertRestriction( $blockId, $type, $value ) {
590 $this->db
->insert( 'ipblocks_restrictions', [
591 'ir_ipb_id' => $blockId,
593 'ir_value' => $value,
597 protected function resetTables() {
598 $this->db
->delete( 'ipblocks', '*', __METHOD__
);
599 $this->db
->delete( 'ipblocks_restrictions', '*', __METHOD__
);